package mas.experiment_2_wordbook.login;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import mas.experiment_2_wordbook.MainActivity;
import mas.experiment_2_wordbook.R;
import mas.experiment_2_wordbook.diary.Add_diary;

public class Edit extends AppCompatActivity {
    //可以加一个保存按钮
    private EditText author;
    private EditText age;
    private EditText like;
    private EditText pwt1;
    private EditText pwt2;
    private Button save;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private String aName;
    private String sexual;
    private String ages;
    private String pwd;
    private String pwd2;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit);

        sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);
        editor = sharedPreferences.edit();

        author = findViewById(R.id.edt_setusername);
        like = findViewById(R.id.like);
        age = findViewById(R.id.edt_setage);
        pwt1 = findViewById(R.id.edt_pwt1);
        pwt2 = findViewById(R.id.edt_pwt2);
        save = findViewById(R.id.saveEdit);
        aName = sharedPreferences.getString("author", null);
        sexual = sharedPreferences.getString("like", null);
        ages = sharedPreferences.getString("age", null);

        if (aName==null||aName.equals("")) {
            author.setText("张硕喆");
        } else {
            author.setText(aName);
        }

        like.setText(sexual);
        age.setText(ages);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pwd = pwt1.getText().toString();
                pwd2 = pwt2.getText().toString();
                if (pwd != null && !pwd.equals("")) {
                    if (!pwd.equals(pwd2)) {
                        Toast.makeText(Edit.this, "两次密码不同", Toast.LENGTH_SHORT).show();
                        pwt2.setText("");
                    } else {
                        AlertDialog.Builder dialog = new AlertDialog.Builder(Edit.this);
                        dialog.setTitle("是否确定修改密码和个人信息");
                        dialog.setCancelable(false);
                        dialog.setMessage("确定要修改个人密码吗？");
                        dialog.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                editor.putString("password", pwt1.getText().toString());
                                editor.putBoolean("isSavePassWord",false);
                                saveAuthorInfo();
                                Toast.makeText(Edit.this,"保存成功",Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(Edit.this,Login.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                startActivity(intent);
                            }
                        });
                        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                editor.clear();
                                Toast.makeText(Edit.this,"取消保存",Toast.LENGTH_SHORT).show();
                            }
                        });
                        dialog.show();
                    }
                }else{
                    saveAuthorInfo();
                    Toast.makeText(Edit.this,"个人信息保存完成",Toast.LENGTH_SHORT).show();
//                    Intent intent = new Intent(Edit.this,MainActivity.class);
//                    startActivity(intent);
                    finish();
                }
//                ProgressDialog progressDialog = new ProgressDialog(Edit.this);
//                progressDialog.setTitle("正在提交保存请稍等");
//                progressDialog.setMessage("加载中.......");
//                progressDialog.setCancelable(true);progressDialog.show();
            }
        });
    }
    /**
     * 保存到sharedPreferences中
     */
    private void saveAuthorInfo(){
        //save
        editor.putString("author", author.getText().toString());
        editor.putString("age", age.getText().toString());
        editor.putString("like", like.getText().toString());
        //editor.putString("password",pwt1.getText().toString());
        editor.commit();
    }
}
